﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace TuyennvLibrary.Reflection
{
    public class DescriptionMethod
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string LinkDev { get; set; }
        public string Description { get; set; }
        public Type DeclaringType { get; set; }
        public Boolean Inherit { get; set; }
        public Boolean Expire { get; set; }
        public string AndroidCode { get; set; }
        public string WinphoneCode { get; set; }
        public string WAPCode { get; set; }
        public DescriptionMethod()
        {
            this.Name = string.Empty;
            this.LinkDev = string.Empty;
            this.Description = string.Empty;
            Inherit = false;
            this.AndroidCode = string.Empty;
            this.WinphoneCode = string.Empty;
            this.WAPCode = string.Empty;
            this.Expire = false;
        }
        public DescriptionMethod(MethodInfo method)
            : this()
        {
            setAndroidCode(method);
            setWAPCode(method);
        }
        public void setAndroidCode(MethodInfo method)
        {
            ParameterInfo[] parammeterArr = method.GetParameters();
            string contentParams = "";
            string parameters = "";
            string listParams = "";
            string listValueParams = "";
            string content = "";


            contentParams += "ApiParams params = new ApiParams();\n";
            contentParams += string.Format("params.addParams(\"cmd\", \"{0}\");\n", method.Name);

            for (int i = 0; i < parammeterArr.Length; i++)
            {
                parameters += string.Format("{0} {1}{2}", getParameterType(parammeterArr[i].ParameterType), parammeterArr[i].Name, (i < parammeterArr.Length - 1) ? ", " : "");
                listParams += string.Format("{0}{1}", parammeterArr[i].Name, (i < parammeterArr.Length - 1) ? ", " : "");
                listValueParams += string.Format("params.addParams(\"{0}\", {1});\n", parammeterArr[i].Name, getParameterValue(parammeterArr[i]));
            }
            content += "<strong>Code for Interface</strong>\n";
            content += "void " + method.Name + "(" + ((!string.IsNullOrEmpty(parameters)) ? parameters + ", " : "") + " RequestListener listener);\n";
            if (parameters.Length > 0)
            {
                listParams = listParams.TrimAll();
                contentParams += string.Format("params.addParams(\"params\", \"{0}\");\n", listParams);
            }
            contentParams += listValueParams;
            string headMethod = "public void " + method.Name + "(" + ((!string.IsNullOrEmpty(parameters)) ? parameters + "," : "") + "final RequestListener listener) {\n";
            content += "<strong>Code for Call Api</strong>\n";
            content += headMethod;

            content += contentParams + "\n";
            content += "BaseApi api= new BaseApi(params, new ApiResponse() {\n"
                        + "@Override\n"
                        + "public void execResponse(JSONObject response) {\n"
                        + "try {\n"
                        + "//Code for response in here\n"
                        + "} catch (Exception e) {\n"
                        + "listener.fail(e.getMessage());\n"
                        + "}\n"
                        + "}\n"
                    + "}){\n"
                        + "@Override\n"
                        + "public void onError(VolleyError error) {\n"
                            + "// TODO Auto-generated method stub\n"
                        + "}\n"
                    + "};\n"
                    + "api.run(listener);\n";

            content += "}\n";
            //Code for in Asyntask
            content += "<strong>Code in Asyntask: Example Loader</strong>\n";
            content += "public JSONObject " + method.Name + "(" + ((!string.IsNullOrEmpty(parameters)) ? parameters + "," : "") + "final RequestListener listener) {\n"; ;
            content += contentParams + "\n";
            content += "BaseApi api = new BaseApi(params) {\n"
                    + "@Override\n"
                    + "public void onError(VolleyError error) {\n"
                        + "// TODO Auto-generated method stub\n"
                    + "}\n"
                    + "};\n"
                    + "try {\n"
                        + "JSONObject json = api.postData(listener);\n"
                        + "return json;\n"
                // Continue Code in here\n"
                    + "} catch (Exception e) {\n"
                        + "listener.fail(e.getMessage());\n"
                    + "}\n"
                    +"return null;\n";
            content += "}\n";
            this.AndroidCode = content;
        }
        private string getParameterType(Type type)
        {
            if (type == typeof(int))
            {
                return "int";
            }
            if (type == typeof(Int16))
            {
                return "int";
            }
            if (type == typeof(Int32))
            {
                return "int";
            }
            if (type == typeof(Int64))
            {
                return "int";
            }
            return type.Name;
        }
        private string getParameterValue(ParameterInfo param)
        {
            string type = getParameterType(param.ParameterType);
            type = type.ToLower();
            if (type == "long" || type == "int" || type == "double")
            {
                return param.Name+"+\"\"";
            }
            return param.Name;
        }
        public void setWAPCode(MethodInfo method)
        {
            ParameterInfo[] parammeterArr = method.GetParameters();
            string contentParams = "";
            string parameters = "";
            string listParams = "";
            string listValueParams = "";
            string content = "";
            contentParams += " ApiParam param = new ApiParam();\n";
            contentParams += string.Format("param.Add(\"cmd\", \"{0}\");\n", method.Name);
            for (int i = 0; i < parammeterArr.Length; i++)
            {
                parameters += string.Format("{0} {1}{2}", getParameterType(parammeterArr[i].ParameterType).ToLower(), parammeterArr[i].Name, (i < parammeterArr.Length - 1) ? ", " : "");
                listParams += string.Format("{0}{1}", parammeterArr[i].Name, (i < parammeterArr.Length - 1) ? ", " : "");
                listValueParams += string.Format("param.Add(\"{0}\", {0});\n", parammeterArr[i].Name, parammeterArr[i].Name);
            }
            if (parameters.Length > 0)
            {
                listParams = listParams.TrimAll();
                contentParams += string.Format("param.Add(\"params\", \"{0}\");\n", listParams);
            }
            contentParams += listValueParams;
            string headMethod = "public string " + method.Name + "(" + parameters + ") {\n";
            content += headMethod;
            content += contentParams + "\n";
            content += "BaseApi api = new BaseApi(config);\n"
                    + "string response = api.RunPOST(param);\n"
                    + "//Custome response in here\n"
                    + "return response;\n";
            content += "}\n";
            this.WAPCode = content;
        }
    }
}
